package org.openrbac.rbac.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.openrbac.rbac.entity.Order;
import org.openrbac.rbac.entity.OrderDetail;
import org.openrbac.rbac.entity.Product;
import org.openrbac.rbac.mapper.OrderDetailMapper;
import org.openrbac.rbac.mapper.ProductMapper;
import org.openrbac.rbac.service.OrderService;
import org.openrbac.rbac.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 梁富贵
* @description 针对表【order】的数据库操作Service实现
* @createDate 2024-06-07 17:58:01
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public boolean addOrder(Order order) {
        order.setId( IdUtil.getSnowflake().nextId());
        OrderDetail orderDetail = new OrderDetail();
        castOrderDetail(order,orderDetail);

        return this.baseMapper.insert(order) >= 1 && orderDetailMapper.insert(orderDetail) >= 1;
    }

    //将订单 转换 成订单详情
    @Override
    public void castOrderDetail(Order order, OrderDetail orderDetail) {
        orderDetail.setId( (int) IdUtil.getSnowflake().nextId());
        orderDetail.setOid( order.getId());
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",order.getProductId());
        orderDetail.setGoodsName(productMapper.selectOne(queryWrapper).getName());
        orderDetail.setGoodsId(order.getProductId());
        orderDetail.setNum(order.getNumber());
        orderDetail.setUnivalence(order.getPrice());
        orderDetail.setTotalAmount(order.getPrice()*order.getNumber());
        orderDetail.setColor(order.getColor());
        orderDetail.setSize(order.getSize());
        orderDetail.setStatus("已支付");
        orderDetail.setNotes(order.getNote());
    }
}




